package com.example.kotlincomponent

import android.os.Bundle
import android.view.ContextMenu
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_menu.*

class MenuActivity: BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu)
        //不知道为什么这一行没用
        btn_menu_show_cm.setOnCreateContextMenuListener(this)

//        registerForContextMenu(btn_menu_show_cm)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return true
    }

    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
        menu?.add(0,1,0,"添加")
        menu?.add(0,2,0,"删除")

    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            1-> Toast.makeText(this,"点击了添加",Toast.LENGTH_SHORT).show()
            2->Toast.makeText(this,"点击了删除",Toast.LENGTH_SHORT).show()
        }
        return true
    }
}